package com.yyn.pigeon.controller;


import com.yyn.pigeon.config.R;
import com.yyn.pigeon.entity.Goods;
import com.yyn.pigeon.entity.ShoppingCart;
import com.yyn.pigeon.service.OrdersService;
import com.yyn.pigeon.service.ShoppingCartService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author yyn
 * @since 2023-03-16
 */
@RestController
@RequestMapping("/pigeon/shopping-cart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @ApiOperation("加入购物车 check=1 +1  =0 -1    ")
    @PostMapping("toShoppingCartUserId")
    public R toShoppingCartUserId(@RequestBody Goods goods, String userId,int check){
        shoppingCartService.toShoppingCartMapperByUserId(goods,userId,check);
        return null;
    }
    @ApiOperation("返回所有订单数据+总额")
    @PostMapping("getOrders")
    public R getOrders(String userId){
        List<ShoppingCart> shoppingCarts = shoppingCartService.getOrders(userId);
        AtomicInteger sum= new AtomicInteger();
        shoppingCarts.stream().map(item->{
            sum.addAndGet(item.getAmount());
            return null;
        }).collect(Collectors.toList());
        return R.success().data("shoppingCarts",shoppingCarts).data("amount",sum);
    }

}

